package com.lanou.sinasports.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ListView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.lanou.sinasports.R;
import com.lanou.sinasports.adapter.SpecialDecAdapter;
import com.lanou.sinasports.beans.GsonSpeDec;
import com.lanou.sinasports.beans.SpecialDec;
import com.lanou.sinasports.utils.DoubleCache;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.LessonApplication;
import com.lanou.sinasports.utils.VolleySingleton;


import java.util.ArrayList;

import java.util.List;

/**
 * Created by dllo on 16/1/23.
 */
public class SpecailDecActivity extends AppCompatActivity {

    private ListView listView;
    private String url;
    private String id;
    private SpecialDecAdapter adapter;
    private TextView tvTitle;
    private TextView tvDesc;
    private TextView tvNum;
    private VolleySingleton queue;
    private NetworkImageView imageView;
    private String num;
    private RequestQueue que;
    private ImageLoader imageLoader;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_detail);
        listView= (ListView) findViewById(R.id.lv_special_detail);


        listView.addHeaderView(LayoutInflater.from(LessonApplication.getContext()).inflate(R.layout.my_header, null));
        tvTitle= (TextView) findViewById(R.id.tv_header_title);
        tvDesc= (TextView) findViewById(R.id.tv_head_desc);
        tvNum= (TextView) findViewById(R.id.tv_head_num);
        imageView= (NetworkImageView) findViewById(R.id.iv_header_pic);



        Intent intent=getIntent();
        id=intent.getStringExtra("id");
        num=intent.getStringExtra("num");
        url="http://platform.sina.com.cn/sports_client/z_feed_list?app_key=2586208540&_version__=3.2.0.14&__os__=android&id=" +
                id+"&pdps_params=%7B%22app%22%3A%7B%22timestamp%22%3A%22"+"%22%2C%22os%22%3A%22Android%22%2C%22model%22%3A%22MI+4LTE%22%2C%22device_type" +
                "%22%3A%224%22%2C%22osv%22%3A%224.4.4%22%2C%22name%22%3A%22cn.com.sina.sports%22%2C%22carrier%22%3A%22%E4%B8%AD%E5%9B%BD%E7%A7%BB%E5%8A%A8%22%2C%22device_id" +
                "%22%3A%22866500020074540%22%2C%22make%22%3A%22MI+4LTE%22%2C%22channel%22%3A%22%22%2C%22connection_type%22%3A%222%22%2C%22" +
                "version%22%3A30200014%2C%22ip%22%3A%22fe80%3A%3A1a59%3A36ff%3Afe27%3A43dd%25wlan0%22%7D%7D&client_deviceid=606f8e0559ca33cfa214e8e14c061c40";

        Log.d("==========", url);

        queue= VolleySingleton.getInstance();
        que= Volley.newRequestQueue(LessonApplication.getContext());
        imageLoader=new ImageLoader(que,new DoubleCache());

        final List<SpecialDec> datas=new ArrayList<>();
        GsonRequest<GsonSpeDec> request=new GsonRequest<GsonSpeDec>(url, GsonSpeDec.class, new Response.Listener<GsonSpeDec>() {
            @Override
            public void onResponse(GsonSpeDec gsonSpeDec) {

                int size=gsonSpeDec.getResult().getData().getFeed().getData().size();
                for(int i=0;i<size;i++){

                    String url=gsonSpeDec.getResult().getData().getFeed().getData().get(i).getImg().getU();
                    String stitle=gsonSpeDec.getResult().getData().getFeed().getData().get(i).getStitle();
                    String summary=gsonSpeDec.getResult().getData().getFeed().getData().get(i).getWapsummary();
                    int  count=gsonSpeDec.getResult().getData().getFeed().getData().get(i).getComment_total();

                    String pic=gsonSpeDec.getResult().getData().getFeed().getPic();
                    String title=gsonSpeDec.getResult().getData().getFeed().getTitle();
                    String desc=gsonSpeDec.getResult().getData().getFeed().getDesc();

                    tvTitle.setText(title);
                    tvDesc.setText(desc);
                    tvNum.setText(num+"订阅");
                    imageView.setDefaultImageResId(R.mipmap.ic_main_tab_news_p);
                    imageView.setErrorImageResId(R.mipmap.ic_comment_cmt_red);
                    imageView.setImageUrl(pic,imageLoader);


                    SpecialDec data=new SpecialDec();
                    data.setUrl(url);
                    data.setStitle(stitle);
                    data.setSummary(summary);
                    data.setCount(count);

                    datas.add(data);
                }


                    adapter=new SpecialDecAdapter(LessonApplication.getContext(),datas);
                    listView.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {


            }
        });

        queue._addRequest(request);



    }
}
